package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomDouble;
import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.EntityBinding;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.MultiRowInsertFeature;
import br.com.telescope.t2.feature.MultiRowUpdateFeature;
import br.com.telescope.t2.feature.QueryFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import solutions.ferpa.ztest.ent.*;

/**
 * Volumes para cálculos de vazões.
 * <p>
 * Interface para manutenção de Volume para cálculo da vazão
 * <P>
 * Telescope origin: ZTEST.CAD_CLASSE_VOLUME_VAZAO
 * <P>
 * Base entity: ZTEST.CLASSE_VOLUME_VAZAO_TEMPO
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadClasseVolumeVazao extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "ZTEST.CAD_CLASSE_VOLUME_VAZAO";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Volumes para cálculos de vazões");
    }
    
    protected EntClasseVolumeVazaoTempo base;
    protected EntClasseModelo clsm;
    protected EntPontoExecucao pexe;
    protected EntClasseVolumeVazaoTempo rec; // NEED_TJW1_COMPATIBILITY
    
    // Record items
    private DomUpper aSiglaVazao;
    private DomDouble aVazaoNominal;
    private DomDouble aVazaoLitrosHora;
    private DomDouble aVolumesLitros;
    private DomDouble aTempoMinimo;
    private DomDouble aTempoMaximo;
    private DomId aClasseModeloId;
    private DomId aId;
    private DomId aPontoExecucaoId;
    private DomUpper aClsmCodigo;
    private DomTextLong aClsmNome;
    private DomText aPexeSigla;
    private DomTextLong aPexeDescricao;
    
    // Query items
    private DomUpper qClsmCodigo;
    private DomDouble qTempoMaximo;
    private DomDouble qVazaoNominal;
    private DomUpper qSiglaVazao;
    private DomText qPexeSigla;
    private DomDouble qVazaoLitrosHora;
    private DomDouble qTempoMinimo;
    private DomDouble qVolumesLitros;
    private DomTextLong qClsmNome;
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , CLSM.CODIGO as CLSM_CODIGO"
                + " , CLSM.DESCRICAO as CLSM_NOME"
                + " , PEXE.SIGLA as PEXE_SIGLA"
                + " , PEXE.DESCRICAO as PEXE_DESCRICAO"
                + " , CLSM.ID as Z_CLSM_ID"
                + " , PEXE.ID as Z_PEXE_ID"
                + " from CLASSE_VOLUME_VAZAO_TEMPO BASE"
                + " join CLASSE_MODELO CLSM on CLSM.ID = BASE.CLASSE_MODELO_ID"
                + " join PONTO_EXECUCAO PEXE on PEXE.ID = BASE.PONTO_EXECUCAO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aSiglaVazao = new DomUpper();
        aSiglaVazao.context(ctx).name("SIGLA_VAZAO").label("Sigla da vazão");
        
        aVazaoNominal = new DomDouble();
        aVazaoNominal.context(ctx).name("VAZAO_NOMINAL").label("Vazão nominal (m3/h)").summary(true);
        
        aVazaoLitrosHora = new DomDouble();
        aVazaoLitrosHora.context(ctx).name("VAZAO_LITROS_HORA").label("Vazão (Litros/hora)").summary(true);
        
        aVolumesLitros = new DomDouble();
        aVolumesLitros.context(ctx).name("VOLUMES_LITROS").label("Volumes (litros)").summary(true);
        
        aTempoMinimo = new DomDouble();
        aTempoMinimo.context(ctx).name("TEMPO_MINIMO").label("Tempo mínimo (segundos)").summary(true);
        
        aTempoMaximo = new DomDouble();
        aTempoMaximo.context(ctx).name("TEMPO_MAXIMO").label("Tempo mínimo (segundos)").summary(true);
        
        aClasseModeloId = new DomId();
        aClasseModeloId.context(ctx).name("CLASSE_MODELO_ID").label("Classe do modelo");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aPontoExecucaoId = new DomId();
        aPontoExecucaoId.context(ctx).name("PONTO_EXECUCAO_ID").label("Ponto de execução");
        
        aClsmCodigo = new DomUpper();
        aClsmCodigo.context(ctx).name("CLSM_CODIGO").label("Classe metrológica");
        aClsmCodigo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT CODIGO as valor"
                + " , CODIGO as pesquisar"
                + " , CODIGO as descricao"
                + " from CLASSE_MODELO CLSM)", "q"));
        aClsmCodigo.setAutocompleteLength(0);
        
        aClsmNome = new DomTextLong();
        aClsmNome.context(ctx).name("CLSM_NOME").label("Descrição da classe metrológica");
        aClsmNome.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from CLASSE_MODELO CLSM)", "q"));
        aClsmNome.setAutocompleteLength(0);
        
        aPexeSigla = new DomText();
        aPexeSigla.context(ctx).name("PEXE_SIGLA").label("Sigla do ponto de ensaio");
        aPexeSigla.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT SIGLA as valor"
                + " , SIGLA as pesquisar"
                + " , SIGLA as descricao"
                + " from PONTO_EXECUCAO PEXE)", "q"));
        aPexeSigla.setAutocompleteLength(2);
        
        aPexeDescricao = new DomTextLong();
        aPexeDescricao.context(ctx).name("PEXE_DESCRICAO").label("Descrição do ponto de ensaio");
        aPexeDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from PONTO_EXECUCAO PEXE)", "q"));
        aPexeDescricao.setAutocompleteLength(2);
        
        
        qClsmCodigo = new DomUpper();
        qClsmCodigo.context(ctx).name("CLSM_CODIGO").label("Classe metrológica");
        qClsmCodigo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT CODIGO as valor"
                + " , CODIGO as pesquisar"
                + " , CODIGO as descricao"
                + " from CLASSE_MODELO CLSM)", "q"));
        qClsmCodigo.setAutocompleteLength(0);
        
        qTempoMaximo = new DomDouble();
        qTempoMaximo.context(ctx).name("TEMPO_MAXIMO").label("Tempo mínimo (segundos)");
        
        qVazaoNominal = new DomDouble();
        qVazaoNominal.context(ctx).name("VAZAO_NOMINAL").label("Vazão nominal (m3/h)");
        
        qSiglaVazao = new DomUpper();
        qSiglaVazao.context(ctx).name("SIGLA_VAZAO").label("Sigla da vazão");
        
        qPexeSigla = new DomText();
        qPexeSigla.context(ctx).name("PEXE_SIGLA").label("Sigla do ponto de ensaio");
        qPexeSigla.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT SIGLA as valor"
                + " , SIGLA as pesquisar"
                + " , SIGLA as descricao"
                + " from PONTO_EXECUCAO PEXE)", "q"));
        qPexeSigla.setAutocompleteLength(2);
        
        qVazaoLitrosHora = new DomDouble();
        qVazaoLitrosHora.context(ctx).name("VAZAO_LITROS_HORA").label("Vazão (Litros/hora)");
        
        qTempoMinimo = new DomDouble();
        qTempoMinimo.context(ctx).name("TEMPO_MINIMO").label("Tempo mínimo (segundos)");
        
        qVolumesLitros = new DomDouble();
        qVolumesLitros.context(ctx).name("VOLUMES_LITROS").label("Volumes (litros)");
        
        qClsmNome = new DomTextLong();
        qClsmNome.context(ctx).name("CLSM_NOME").label("Descrição da classe metrológica");
        qClsmNome.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from CLASSE_MODELO CLSM)", "q"));
        qClsmNome.setAutocompleteLength(0);
    }
    
    @Override
    public void initEntityBindings() {
        EntityBinding binding;
        
        binding = new EntityBinding("ZTEST.CLASSE_MODELO", "CLSM", "Z_CLSM_ID", "ZTEST.CAD_CLASSE_MODELO.VIEW");
        binding.addFields(aClsmCodigo, aClsmNome);
        entityBindings.add(binding);
        
        binding = new EntityBinding("ZTEST.PONTO_EXECUCAO", "PEXE", "Z_PEXE_ID", "ZTEST.CAD_PONTO_EXECUCAO.VIEW");
        binding.addFields(aPexeSigla, aPexeDescricao);
        entityBindings.add(binding);
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntClasseVolumeVazaoTempo getRecord() {
        if (base == null) {
            base = new EntClasseVolumeVazaoTempo(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aSiglaVazao.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "SIGLA_VAZAO, PEXE_SIGLA, CLSM_CODIGO, VAZAO_NOMINAL";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // CLASSE_MODELO_ID
        try {
            if (form.hasFields("CLSM_CODIGO")) {
                query = createQuery()
                        .select("CLSM.ID")
                        .from("CLASSE_MODELO", "CLSM")
                        .filter("CLSM.CODIGO", form.get("CLSM_CODIGO"))
                        ;
                base.setClasseModeloId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // PONTO_EXECUCAO_ID
        try {
            if (form.hasFields("PEXE_SIGLA")) {
                query = createQuery()
                        .select("PEXE.ID")
                        .from("PONTO_EXECUCAO", "PEXE")
                        .filter("PEXE.SIGLA", form.get("PEXE_SIGLA"))
                        ;
                base.setPontoExecucaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // CLASSE_MODELO_ID
        try {
            if (form.hasFields("CLSM_CODIGO")) {
                query = createQuery()
                        .select("CLSM.ID")
                        .from("CLASSE_MODELO", "CLSM")
                        .filter("CLSM.CODIGO", form.get("CLSM_CODIGO"))
                        ;
                base.setClasseModeloId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // PONTO_EXECUCAO_ID
        try {
            if (form.hasFields("PEXE_SIGLA")) {
                query = createQuery()
                        .select("PEXE.ID")
                        .from("PONTO_EXECUCAO", "PEXE")
                        .filter("PEXE.SIGLA", form.get("PEXE_SIGLA"))
                        ;
                base.setPontoExecucaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="QUERY">
    public class FQuery extends QueryFeature {
        
        @Override
        public void initForm() {
            addField(qSiglaVazao, "opt");
            addField(qPexeSigla, "opt");
            addField(qClsmCodigo, "opt");
            addField(qClsmNome, "opt");
            addField(qVazaoNominal, "opt");
            addField(qVazaoLitrosHora, "opt");
            addField(qVolumesLitros, "opt");
            addField(qTempoMinimo, "opt");
            addField(qTempoMaximo, "opt");
        }
        
        {
            setFeatureName("QUERY");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aSiglaVazao, "view");
            addField(aPexeSigla, "view");
            addField(aClsmCodigo, "view");
            addField(aVazaoNominal, "view");
            addField(aVazaoLitrosHora, "view");
            addField(aVolumesLitros, "view");
            addField(aTempoMinimo, "view");
            addField(aTempoMaximo, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aSiglaVazao, "view");
            addField(aPexeSigla, "view");
            addField(aPexeDescricao, "view");
            addField(aClsmCodigo, "view");
            addField(aClsmNome, "view");
            addField(aVazaoNominal, "view");
            addField(aVazaoLitrosHora, "view");
            addField(aVolumesLitros, "view");
            addField(aTempoMinimo, "view");
            addField(aTempoMaximo, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends MultiRowInsertFeature {
        
        @Override
        public void initForm() {
            aSiglaVazao.setOptional(false);
            addField(aSiglaVazao, "rqd");
            aPexeSigla.setOptional(false);
            addField(aPexeSigla, "rqd");
            aPexeDescricao.setEditable(false);
            addField(aPexeDescricao, "view");
            aClsmCodigo.setOptional(false);
            addField(aClsmCodigo, "rqd");
            aClsmNome.setEditable(false);
            addField(aClsmNome, "view");
            aVazaoNominal.setOptional(false);
            addField(aVazaoNominal, "rqd");
            aVazaoLitrosHora.setOptional(false);
            addField(aVazaoLitrosHora, "rqd");
            aVolumesLitros.setOptional(false);
            addField(aVolumesLitros, "rqd");
            aTempoMinimo.setOptional(false);
            addField(aTempoMinimo, "rqd");
            aTempoMaximo.setOptional(false);
            addField(aTempoMaximo, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
            setRows(15);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends MultiRowUpdateFeature {
        
        @Override
        public void initForm() {
            aSiglaVazao.setOptional(false);
            addField(aSiglaVazao, "rqd");
            aPexeSigla.setOptional(false);
            addField(aPexeSigla, "rqd");
            aPexeDescricao.setEditable(false);
            addField(aPexeDescricao, "view");
            aClsmCodigo.setOptional(false);
            addField(aClsmCodigo, "rqd");
            aClsmNome.setEditable(false);
            addField(aClsmNome, "view");
            aVazaoNominal.setOptional(false);
            addField(aVazaoNominal, "rqd");
            aVazaoLitrosHora.setOptional(false);
            addField(aVazaoLitrosHora, "rqd");
            aVolumesLitros.setOptional(false);
            addField(aVolumesLitros, "rqd");
            aTempoMinimo.setOptional(false);
            addField(aTempoMinimo, "rqd");
            aTempoMaximo.setOptional(false);
            addField(aTempoMaximo, "rqd");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
            setRows(15);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aSiglaVazao, "view");
            addField(aPexeSigla, "view");
            addField(aClsmCodigo, "view");
            addField(aVazaoNominal, "view");
            addField(aVazaoLitrosHora, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CLSM_CODIGO_LOOKUP">
    public class FClsmCodigoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aClsmCodigo.setEditable(true);
            addField(aClsmCodigo, "opt");
            aClsmNome.setEditable(false);
            addField(aClsmNome, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("CLSM.CODIGO as CLSM_CODIGO")
                    .select("CLSM.DESCRICAO as CLSM_NOME")
                    .from("CLASSE_MODELO", "CLSM")
                    ;
        }
        
        {
            setFeatureName("CLSM_CODIGO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="PEXE_SIGLA_LOOKUP">
    public class FPexeSiglaLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aPexeSigla.setEditable(true);
            addField(aPexeSigla, "opt");
            aPexeDescricao.setEditable(false);
            addField(aPexeDescricao, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("PEXE.SIGLA as PEXE_SIGLA")
                    .select("PEXE.DESCRICAO as PEXE_DESCRICAO")
                    .from("PONTO_EXECUCAO", "PEXE")
                    ;
        }
        
        {
            setFeatureName("PEXE_SIGLA_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FQuery());
        addFeature(new FList());
        addFeature(new FView());
        addFeature(new FInsert());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FClsmCodigoLookup());
        addFeature(new FPexeSiglaLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "QUERY";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getSiglaVazao() {
        return aSiglaVazao.getValue();
    }
    
    public void setSiglaVazao(String value) {
        this.aSiglaVazao.setValue(value);
    }
    
    public Double getVazaoNominal() {
        return aVazaoNominal.getValue();
    }
    
    public void setVazaoNominal(Double value) {
        this.aVazaoNominal.setValue(value);
    }
    
    public Double getVazaoLitrosHora() {
        return aVazaoLitrosHora.getValue();
    }
    
    public void setVazaoLitrosHora(Double value) {
        this.aVazaoLitrosHora.setValue(value);
    }
    
    public Double getVolumesLitros() {
        return aVolumesLitros.getValue();
    }
    
    public void setVolumesLitros(Double value) {
        this.aVolumesLitros.setValue(value);
    }
    
    public Double getTempoMinimo() {
        return aTempoMinimo.getValue();
    }
    
    public void setTempoMinimo(Double value) {
        this.aTempoMinimo.setValue(value);
    }
    
    public Double getTempoMaximo() {
        return aTempoMaximo.getValue();
    }
    
    public void setTempoMaximo(Double value) {
        this.aTempoMaximo.setValue(value);
    }
    
    public Long getClasseModeloId() {
        return aClasseModeloId.getValue();
    }
    
    public void setClasseModeloId(Long value) {
        this.aClasseModeloId.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getPontoExecucaoId() {
        return aPontoExecucaoId.getValue();
    }
    
    public void setPontoExecucaoId(Long value) {
        this.aPontoExecucaoId.setValue(value);
    }
    
    public String getClsmCodigo() {
        return aClsmCodigo.getValue();
    }
    
    public void setClsmCodigo(String value) {
        this.aClsmCodigo.setValue(value);
    }
    
    public String getClsmNome() {
        return aClsmNome.getValue();
    }
    
    public void setClsmNome(String value) {
        this.aClsmNome.setValue(value);
    }
    
    public String getPexeSigla() {
        return aPexeSigla.getValue();
    }
    
    public void setPexeSigla(String value) {
        this.aPexeSigla.setValue(value);
    }
    
    public String getPexeDescricao() {
        return aPexeDescricao.getValue();
    }
    
    public void setPexeDescricao(String value) {
        this.aPexeDescricao.setValue(value);
    }
    public String getQuerySiglaVazao() {
        return qSiglaVazao.getQueryValue();
    }
    
    public void setQuerySiglaVazao(String value) {
        this.qSiglaVazao.setQueryValue(value);
    }
    public String getQueryPexeSigla() {
        return qPexeSigla.getQueryValue();
    }
    
    public void setQueryPexeSigla(String value) {
        this.qPexeSigla.setQueryValue(value);
    }
    public String getQueryClsmCodigo() {
        return qClsmCodigo.getQueryValue();
    }
    
    public void setQueryClsmCodigo(String value) {
        this.qClsmCodigo.setQueryValue(value);
    }
    public String getQueryClsmNome() {
        return qClsmNome.getQueryValue();
    }
    
    public void setQueryClsmNome(String value) {
        this.qClsmNome.setQueryValue(value);
    }
    public String getQueryVazaoNominal() {
        return qVazaoNominal.getQueryValue();
    }
    
    public void setQueryVazaoNominal(String value) {
        this.qVazaoNominal.setQueryValue(value);
    }
    public String getQueryVazaoLitrosHora() {
        return qVazaoLitrosHora.getQueryValue();
    }
    
    public void setQueryVazaoLitrosHora(String value) {
        this.qVazaoLitrosHora.setQueryValue(value);
    }
    public String getQueryVolumesLitros() {
        return qVolumesLitros.getQueryValue();
    }
    
    public void setQueryVolumesLitros(String value) {
        this.qVolumesLitros.setQueryValue(value);
    }
    public String getQueryTempoMinimo() {
        return qTempoMinimo.getQueryValue();
    }
    
    public void setQueryTempoMinimo(String value) {
        this.qTempoMinimo.setQueryValue(value);
    }
    public String getQueryTempoMaximo() {
        return qTempoMaximo.getQueryValue();
    }
    
    public void setQueryTempoMaximo(String value) {
        this.qTempoMaximo.setQueryValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/ztest/cad_classe_volume_vazao.js";
    }
}
